package ko;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import ko.util.ParamImage;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.util.Log;

public class KSources extends KEvent {

	private static ArrayList<ArrayList<ArrayList<Bitmap>>> bitmaps = new ArrayList<ArrayList<ArrayList<Bitmap>>>();
	private static Hashtable<String, KSound> sounds = new Hashtable<String, KSound>();

	private static int fileLength;
	private static int fileIndex = 0;
	private List<ParamImage> imagesUrl;
	private List<Integer> soundsUrl;

	public KSources() {
		fileLength = 0;
		imagesUrl = new Vector<ParamImage>();
		soundsUrl = new Vector<Integer>();
	}

	public void addImages(ParamImage... args) {//Vector<ParamImage>
		fileLength = fileLength + args.length;
		for (ParamImage p : args) {
			imagesUrl.add(p);
		}
	}

	public void addSounds(int... args) {
		fileLength = fileLength += args.length;
		for (int i : args) {
			soundsUrl.add(new Integer(i));
		}
	}

	public void Load() {
		fileIndex = 0;
		for (ParamImage p:imagesUrl) {
			CompleteBitmaps(p);
			fileIndex = fileIndex + 1;
		}
		for (int i:soundsUrl) {
			CompleteSounds(i);
			fileIndex = fileIndex + 1;
		}
		LoadComplete();
	}

	private void CompleteSounds(int i) {
		String name = K.stage.getResources().getResourceEntryName(i);
		KSound sound = new KSound(MediaPlayer.create(K.context, i));
		sounds.put(name, sound);
	}

	private void CompleteBitmaps(ParamImage p) {
		Bitmap bmp = ((BitmapDrawable) K.stage.getResources().getDrawable(p.getSrc())).getBitmap();
		Rect rect = p.getRect();
		
		int maxW = bmp.getWidth() / rect.width();
		int maxH = bmp.getHeight() / rect.height();
		
		ArrayList<ArrayList<Bitmap>> ary = new ArrayList<ArrayList<Bitmap>>();
		Bitmap bitmap;
		int x, y;
		for (int i = 0; i < maxW; i++) {
			ArrayList<Bitmap> als = new ArrayList<Bitmap>();
			for (int j = 0; j < maxH; j++) {
				x = i * rect.width();
				y = j * rect.height();
				
				bitmap = Bitmap.createBitmap(bmp, x, y, rect.width(), rect.height());
				als.add(bitmap);
			}
			ary.add(als);
		}
		bitmaps.add(ary);
	}
	
	public static Bitmap getBitmap() {
		return getBitmapHelper(0,0,0);
	}
	
	public static Bitmap getBitmap(int p1) {
		return getBitmapHelper(p1,0,0);
	}
	
	public static Bitmap getBitmap(int p1, int p2) {
		return getBitmapHelper(p1,p2,0);
	}
	
	public static Bitmap getBitmap(int p1, int p2, int p3) {
		return getBitmapHelper(p1,p2,p3);
	}
	
	private static Bitmap getBitmapHelper(int p1, int p2, int p3) {
		return bitmaps.get(p1).get(p2).get(p3);
	} 
	
	public static void stop(String name) {
		KSound sound = sounds.get(name);
		if (sound != null) {
			sound.stop();
		}
	}
	public static void loop(String name) {
		KSound sound = sounds.get(name);
		if (sound != null) {
			sound.loop(1,1);
		}
	}
	public static void play(String name) {
		playHelper(name,1,1);
	}		
	public static void play(String name,float param1,float param2) {
		playHelper(name,param1,param2);
	}	
	private static void playHelper(String name,float param1,float param2) {
		KSound sound = sounds.get(name);
		if (sound != null) {
			sound.play(param1, param2);
		}
	}
	public static KSound Music(String name) {
		return sounds.get(name);
	}
	

	private void LoadComplete() {
		Log.i("KSource", "dispatchEvent");
		this.dispatchEvent(this, KEvent.COMPLETE);
	}
}
